/*================================================================================
* CopyRight(c)2008, www.k-zone.cn. All rights reserved.
* Code licensed under the BSD License
* Author: Kenshin, Email: fxblog@gmail.com, Blog: http://www.k-zone.cn/zblog/
* Version：v0.1
* Data：2008-2-1
*===============================================================================*/
package com.ken.fanfou.as3.messages
{
	import com.ken.fanfou.as3.FanFouEvent;
	import com.ken.fanfou.as3.FanFouMethod;
	import com.ken.fanfou.as3.Type;
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;

	public class Inbox extends FanFouMethod
	{
		public function Inbox(value:String){
			super();
			_url = value;
		}
		
		/** 
		* 重写FanFouMethod，使其具有详细方法，而方法是针对与inbox而写。
		* 显示用户的私信
		* 
		* @param 饭否默认显示的xml条数、默认是20条
		* @langversion ActionScript 3.0
		* @playerversion Flash 9.0
		*/
		public override function Loader(count:int = 20):void{
			var request:URLRequest = new URLRequest(_url + Type.FANFOU_COUNT + count);
			var loader:URLLoader = new URLLoader();
			loader.load(request);
			loader.addEventListener(IOErrorEvent.IO_ERROR,errorHandler);
			loader.addEventListener(Event.COMPLETE,completeHandler);
		}
		
		/** 
		* 私有方法，如果urlloader出错，则执行这个方法
		* 
		* @langversion ActionScript 3.0
		* @playerversion Flash 9.0
		*/
		private function errorHandler(event:IOErrorEvent):void{
			trace(111111)
		}

		/** 
		* 私有方法，如果读取正确出错，则执行这个方法。并且dispatchEvent一个事件，已被客户端监听。
		* 其中_xml是它的父类FanFouMethod的方法。 
		* 
		* @langversion ActionScript 3.0
		* @playerversion Flash 9.0
		*/
		private function completeHandler(event:Event):void{
			_xml = new XML(event.target.data);
			dispatchEvent(new FanFouEvent(FanFouEvent.COMPLETE));
		}
		
		/** 
		* 重写GetXml，将得到的内容→xml中
		* 
		* @langversion ActionScript 3.0
		* @playerversion Flash 9.0
		*/
		public override function GetXml():XML{
			return _xml;
		}

		/**------------------------------------ inbox.xml的属性 ------------------------------------*/
		/** 
		* inbox返回的XML数组：user的长度
		* 
		* @langversion ActionScript 3.0
		* @playerversion Flash 9.0
		*/
		public override function get XmlLength():uint{
			return _xml.private_message.length();
		}

		/** 
		* inbox返回的XML数组：<user>中的内容
		* 包括：screen_name、id、name、location、description、profile_image_url、url
		* 
		* @return Array
		* @langversion ActionScript 3.0
		* @playerversion Flash 9.0
		* 返回的XML、例如：
    	* <private-messages>
    	*   <private_message>
    	*     <id>144620</id>
    	*     <text>xxxxxxxx</text>
    	*     <sender_id>xxxxx</sender_id>
    	*     <recipient_id>Kenshin.L</recipient_id>
    	*     <created_at>Sat Nov 03 16:22:31 +0000 2007</created_at>
    	*     <sender_screen_name>awflasher</sender_screen_name>
    	*     <recipient_screen_name>Kenshin</recipient_screen_name>
    	*   </private_message>
    	* </private-messages>
		*/
		public override function get XmlContent():Array{
			for(var i:uint = 0;i < _xml.private_message.length();i++ ){
				_array.push({id:_xml.private_message[i].id,
				             text:_xml.private_message[i].text,
				             sender_id:_xml.private_message[i].sender_id,
				             recipient_id:_xml.private_message[i].recipient_id,
				             created_at:_xml.private_message[i].created_at,
				             sender_screen_name:_xml.private_message[i].sender_screen_name,
				             recipient_screen_name:_xml.private_message[i].recipient_screen_name
				         });
			}
			return _array;
		}
	}
}